package com.dense.kuiniu.shelfcard.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.yokeyword.fragmentation.SupportFragment;

/**
 * @author Administrator
 * @date 2020/8/6
 */
public abstract class BaseFragment extends SupportFragment implements View.OnClickListener {
    protected View mRootView = null;
    protected boolean mViewCreated = false;
    protected Unbinder mUnBinder = null;

    protected abstract int getLayoutRes();

    protected abstract void init();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mRootView == null) {
            final int layoutId = getLayoutRes();
            if (layoutId > 0) {
                mRootView = inflater.inflate(getLayoutRes(), container, false);
            }
        }
        mUnBinder = ButterKnife.bind(this, mRootView);
        mViewCreated = true;
        init();
        return mRootView;
    }

    protected boolean onClick1(final View v) {
        return false;
    }


    protected void onClick2(final View v) {
    }


    @Override
    public void onClick(final View v) {
        if (!onClick1(v)) {
            ClickHelper.onlyFirstSameView(v, new ClickHelper.Callback() {
                @Override
                public void onClick(View view) {
                    onClick2(view);
                }
            });
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mRootView = null;
        mViewCreated = false;
        if (mUnBinder != null) {
            mUnBinder.unbind();
        }
    }
}
